package org.tarena.dang.action.main;

import java.sql.SQLException;
import java.util.List;

import org.tarena.dang.action.BaseAction;
import org.tarena.dang.entity.Book;
import org.tarena.dang.entity.Categrory;
import org.tarena.dang.service.BookService;
import org.tarena.dang.service.BookServiceImp;
import org.tarena.dang.service.CategoryService;
import org.tarena.dang.service.CategoryServiceImp;

public class BookListAction extends BaseAction {
	private int parentId;
	private int cid;//category id
	private int totalNum;//全部记录数
	private List<Book> books;//中间产品数据
	private List<Categrory> cats;//左侧分类列表
	private int pageSize = 4;
	private int currentPage = 1;//当前页码  默认1
	private int totalPage;//总页码数
	private int totalSize;//总记录数
	private String sort = "timeUp";//默认排序按时间
	private String classes;//显示类别如 小说  青春 
	
	
	public String execute(){
		CategoryService categoryService = new CategoryServiceImp();
		BookService bookService = new BookServiceImp();
		try {
			cats = categoryService.findByParentId(parentId);
			System.out.println("booklist查询到的记录数是："+cats.size());
			//books = bookService.findBySort(cid,currentPage,pageSize,sort);
			books = bookService.findByBook(parentId,cid,currentPage,pageSize,sort);
			//登录进来parentId=0 显示当前所在目录
			if(parentId!=0){
				classes = categoryService.findByParetIdClass(parentId);
			}
			//获取分页信息
			totalSize = bookService.findByTotalSize(cid,parentId);
			//totalSize = bookService.findByCiddAll(cid);
			totalPage = bookService.getTotalPage(totalSize,pageSize);
			System.out.println("查询到的Book数是："+totalSize);
			System.out.println("每页显示--"+pageSize+"--条，共--"+totalPage+"--页。");
			for(Categrory c:cats){
				totalNum+=c.getPnum();
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return "success";
	}
	
	
	
	public List<Categrory> getCats() {
		return cats;
	}

	public void setCats(List<Categrory> cats) {
		this.cats = cats;
	}
	public int getParentId() {
		return parentId;
	}
	public void setParentId(int parentId) {
		this.parentId = parentId;
	}
	
	public int getCid() {
		return cid;
	}


	public void setCid(int cid) {
		this.cid = cid;
	}

	public String getClasses() {
		return classes;
	}



	public void setClasses(String classes) {
		this.classes = classes;
	}



	public String getSort() {
		return sort;
	}



	public void setSort(String sort) {
		this.sort = sort;
	}
	public int getTotalNum() {
		return totalNum;
	}
	public void setTotalNum(int totalNum) {
		this.totalNum = totalNum;
	}


	public List<Book> getBooks() {
		return books;
	}


	public void setBooks(List<Book> books) {
		this.books = books;
	}
	
	public int getPageSize() {
		return pageSize;
	}



	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}



	public int getCurrentPage() {
		return currentPage;
	}



	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;
	}



	public int getTotalPage() {
		return totalPage;
	}



	public void setTotalPage(int totalPage) {
		this.totalPage = totalPage;
	}



	public int getTotalSize() {
		return totalSize;
	}



	public void setTotalSize(int totalSize) {
		this.totalSize = totalSize;
	}



	
}
